//index.js
//获取应用实例
const app = getApp();
const utils = require('../../utils/util');

Page({
    data: {
        title: "发布时间",
        _num: "0",
        list: [],
        page: 1,
        status: 'P',
        taskNumber: [],
        showPopup: false,
        agencyName: null,
        name: null
    },
    onLoad: function (options) {
        if (options.status && options.status != "") {
            this.setData({
                status: options.status
            })
            if (options.status == "P") {
                this.setData({
                    _num: 0
                })
            }
            if (options.status == "W") {
                this.setData({
                    _num: 1
                })
            }
            if (options.status == "V") {
                this.setData({
                    _num: 2
                })
            }
            if (options.status == "B") {
                this.setData({
                    _num: 3
                })
            }
            if (options.status == "R") {
                this.setData({
                    _num: 4
                })
            }
        }
    },
    //获取各个状态下的任务数量
    getTaskNumber: function () {
        utils.FetchRequest('/v1/api/task/count_task_by_status', {}, 'get').then((res) => {
            if (res.data.status == 200) {
                if (res.data.data.length > 5) {
                    let num = [];
                    let s = ["P", "W", "V", "B", "R"];
                    let a = res.data.data;
                    for (let i = 0; i < s.length; i++) {
                        for (let b = 0; b < a.length; b++) {
                            if (a[b].status == s[i]) {
                                num.push(a[b]);
                            }
                        }
                    }
                    this.setData({
                        taskNumber: num
                    })
                }
            } else {
                wx.showToast({
                    title: res.data.message,
                    icon: 'none',
                    duration: 2000
                });
            }
        })
    },
    togger: function (e) {
        var index = e.currentTarget.dataset.index;
        this.setData({
            _num: index
        })
        if (this.data._num == "0") {
            this.setData({
                title: "发布时间",
                status: "P"
            })
        }
        if (this.data._num == "1") {
            this.setData({
                title: "接单时间",
                status: "W"
            })
        }
        if (this.data._num == "2") {
            this.setData({
                title: "提交时间",
                status: "V"
            })
        }
        if (this.data._num == "3") {
            this.setData({
                title: "修改时间",
                status: "B"
            })
        }
        if (this.data._num == "4") {
            this.setData({
                title: "拒绝时间",
                status: "R"
            })
        }
        this.setData({
            page: 1
        });
        this.getListInternal();
        this.getTaskNumber();
    },
    //下拉刷新
    onPullDownRefresh: function () {
        // 显示顶部刷新图标
        wx.showNavigationBarLoading();
        this.setData({
           page: 1
        });
        this.getListInternal();
        this.getTaskNumber();
    },
    //上拉加载
    onReachBottom: function () {
        var that = this;
        // 显示加载图标
        wx.showLoading({
            title: '正在加载中'
        });
        // 页数+1
        that.setData({
            page: that.data.page + 1
        });
        utils.FetchRequest(
            '/v1/api/task/list?status=' + that.data.status + '&pageNo=' + that.data.page + '&pageSize=10', {}, 'get').then((res) => {
            if (res.data.status == 200) {
                if (res.data.data.length != 0) {
                    var listTwo = res.data.data;
                    that.setData({
                        list: that.data.list.concat(listTwo)
                    })
                } else {
                    wx.showToast({
                        title: '没有更多了',
                        icon: 'none',
                        duration: 2000
                    });
                }
            } else {
                wx.showToast({
                    title: res.data.message,
                    icon: 'none',
                    duration: 2000
                });
            }
        });
    },
    seeDetail: function (e) {
        var taskId = e.currentTarget.dataset.taskid.taskId;
        var taskStatus = e.currentTarget.dataset.taskid.taskStatus;
        if (taskStatus == "P") {
            wx.navigateTo({
                url: '/pages/receipt/receipt?taskId=' + taskId
            })
        }
        if (taskStatus == "W") {
            wx.navigateTo({
                url: '/pages/operation/operation?taskId=' + taskId
            })
        }
        if (taskStatus == "V") {
            wx.navigateTo({
                url: '/pages/acceptance/acceptance?taskId=' + taskId
            })
        }
        if (taskStatus == "R") {
            wx.navigateTo({
                url: '/pages/refuse/refuse?taskId=' + taskId
            })
        }
    },
    getList: function () {
        var that = this;
        wx.checkSession({
            success() {
                wx.getStorage({
                    key: 'userInfo',
                    success: function (res) {
                        var userInfo = res.data;
                        wx.setStorageSync('userInfo', res.data);
                        wx.setStorageSync('token', res.data.token);
                        if (userInfo == null) {
                            wx.navigateTo({
                                url: "/pages/login/login"
                            });
                            return;
                        }
                        if (userInfo.register) {
                            wx.navigateTo({
                                url: "/pages/login/login"
                            });
                            return;
                        }
                        utils.FetchRequest('/v1/api/userInfo', {}, "get").then((res) => {
                            wx.setStorageSync('info', res.data.data);
                            var info = res.data.data;
                            if (userInfo.register) {
                                wx.navigateTo({
                                    url: "/pages/login/login"
                                });
                            } else if (info.bankcardResult != 2) {
                                wx.redirectTo({
                                    url: '/pages/bindCard/bindCard'
                                })
                            } else if (info.idcardResult != 2) {
                                wx.redirectTo({
                                    url: '/pages/authentication/authentication'
                                })
                            } else {
                                that.getListInternal();
                            }
                        });
                    },
                    fail: function () {
                        wx.navigateTo({
                            url: "../login/login"
                        })
                    }
                });
            },
            fail() {
                console.log("登录失败");
                wx.navigateTo({
                    url: "../login/login"
                })
            }
        });

    },
    getListInternal() {
        wx.showLoading({
            title: '正在加载中！',
        });
        utils.FetchRequest(
            '/v1/api/task/list?status=' + this.data.status + '&pageNo=' + this.data.page + '&pageSize=10', {}, 'get').then((res) => {
            if (res.data.status == 200) {
                this.setData({
                    list: res.data.data
                });
                // 隐藏导航栏加载框
                wx.hideNavigationBarLoading();
                // 停止下拉动作
                wx.stopPullDownRefresh();
                wx.hideLoading();
            } else {
                wx.showToast({
                    title: res.data.message,
                    icon: 'none',
                    duration: 2000
                });
                wx.hideLoading();
            }
        })
    },
    showPop: function () {
        this.setData({
            showPopup: false
        });
        wx.navigateTo({
            url: "/pages/confirmationAgreement/confirmationAgreement"
        });
    },
    hidePop: function () {
        this.setData({
            showPopup: false
        })
    },
    getAgreementList: function () {
        wx.showLoading({
            title: '正在加载中！'
        });
        utils.FetchRequest('/v1/api/task/frameAgreementByCkerId', {}, "get").then((res) => {
            if (res.data.data.length != 0) {
                this.setData({
                    showPopup: true,
                    agencyName: res.data.data[0].agencyName,
                    name: res.data.data[0].name,
                });
                wx.hideLoading();
                wx.setStorageSync('agreementUrl', res.data.data);
            } else {
                wx.hideLoading();
            }
        });
    },
    onShow: function () {
        if (app.globalData.status && app.globalData.status != "") {
            this.setData({
                status: app.globalData.status
            })
            if (app.globalData.status == "P") {
                this.setData({
                    _num: 0
                })
            }
            if (app.globalData.status == "W") {
                this.setData({
                    _num: 1
                })
            }
            if (app.globalData.status == "V") {
                this.setData({
                    _num: 2
                })
            }
            if (app.globalData.status == "B") {
                this.setData({
                    _num: 3
                })
            }
            if (app.globalData.status == "R") {
                this.setData({
                    _num: 4
                })
            }
        }
        this.setData({
            page: 1
        });
        this.getList();
        this.getTaskNumber();
        this.getAgreementList();
    }
});